<template>
  <view>
    <view class="my-settle-container">
      <!-- 全选区域 -->
      <label class="radio">
        <radio color="#C00000" :checked="isFullChecked" @click="changeAllState" />
        <text>全选</text>
      </label>

      <!-- 合计区域 -->
      <view class="amount-box">
        合计:
        <text class="amount">￥{{ checkedGoodsAmount }}</text>
      </view>

      <!-- 结算按钮 -->
      <view class="btn-settle" @click="settle">结算{{ checkedCount }}</view>
    </view>
  </view>
</template>

<script>
import { mapGetters, mapMutations, mapState } from 'vuex';
export default {
  name: 'my-settle',
  computed: {
    ...mapGetters('m_cart', ['checkedCount', 'total', 'checkedGoodsAmount']),
    ...mapGetters('m_user', ['addstr']),
    ...mapState('m_user', ['token']),
    ...mapState('m_cart', ['cart']),
    isFullChecked() {
      return this.total === this.checkedCount;
    }
  },
  data() {
    return {
      timer: undefined
    };
  },
  onLoad() {
    if (this.timer !== undefined) {
      clearInterval(this.timer);
    }
  },
  methods: {
    ...mapMutations('m_cart', ['updateAllGoodsState']),
    changeAllState() {
      this.updateAllGoodsState(!this.isFullChecked);
    },
    settle() {
      // 1.有没有勾选
      if (this.checkedCount <= 0) return uni.$showMsg('请选中商品！！！');
      // 2.有没有选择地址
      if (this.addstr === '') return uni.$showMsg('请选择收货地址！！！');
      // 3.判断是否登录
      let seconds = 3;
      if (this.token === '') {
        this.timer = setInterval(() => {
          if (seconds === 0) {
            clearInterval(this.timer);
            // uni.hideToast()
            uni.switchTab({
              url: '/pages/my/my'
            });
          }
          uni.showToast({
            icon: 'none',
            title: `未登录,${seconds--}秒之后跳转到登陆页面`,
            duration: 1500
          });
        }, 1000);
      } else {
        this.payOrder();
      }
    },
    // async payOrder(){
    //   const orderInfo = {
    //       // 开发期间，注释掉真实的订单价格，
    //       // order_price: this.checkedGoodsAmount,
    //       // 写死订单总价为 1 分钱
    //       order_price: 0.01,
    //       consignee_addr: this.addstr,
    //       goods: this.cart.filter(x => x.goods_state).map(x => ({ goods_id: x.goods_id, goods_number: x.goods_count, goods_price: x.goods_price }))
    //     }
    //     // 1.2发请求
    //     let {data:{message,meta:{status}}} = await uni.$http.post('/api/public/v1/my/orders/create')
    //     // 1.3获取订单编号
    //     if(status !==200) return uni.$showMsg('创建订单失败')
    //     let orderNumber = message.order_number
    // }
    async payOrder() {
      //1.创建订单
      //1.1 订单对象
      const orderInfo = {
        // 开发期间，注释掉真实的订单价格，
        // order_price: this.checkedGoodsAmount,
        // 写死订单总价为 1 分钱
        order_price: 0.01,
        consignee_addr: this.addstr,
        goods: this.cart.filter(x => x.goods_state).map(x => ({ goods_id: x.goods_id, goods_number: x.goods_count, goods_price: x.goods_price }))
      };
      //1.2 发请求
      let {
        data: {
          message,
          meta: { status }
        }
      } = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo);
      //1.3 获取订单编号
      if (status !== 200) return uni.$showMsg('创建订单失败');
      let orderNumber = message.order_number;
      console.log('orderNumber', orderNumber);
      //2.预支付
      let {
        data: {
          message: { pay },
          meta: { status: status2 }
        }
      } = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', { order_number: orderNumber });
      if (status2 !== 200) return uni.$showMsg('预支付失败');
      console.log('pay', pay);
      //3.支付
      //3.1调用手机的微信支付(对话框)
      let [err] = await uni.requestPayment(pay);
      if (err) return this.$showMsg('支付失败');
      //3.2查询后台是否支付成功
      let {
        data: {
          meta: { status: status3 }
        }
      } = await uni.$http.post('/api/public/v1/my/orders/chkOrder', { order_number: orderNumber });
      if (status3 !== 200) return uni.$showMsg('支付失败');
      uni.showToast({
        icon: 'success',
        title: '支付成功'
      });
    }
  }
};
</script>

<style lang="scss">
.my-settle-container {
  position: fixed;
  // 在网页端
  /*  #ifdef H5 */
  bottom: 50px;
  /*  #endif  */
  // 在小程序端
  /*  #ifdef MP-WEIXIN */
  bottom: 0;
  /*  #endif  */
  left: 0;
  width: 100%;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: white;
  padding-left: 5px;
  font-size: 14px;
  .radio {
    display: flex;
    align-items: center;
  }
  .amount {
    color: #c00000;
  }
  .btn-settle {
    height: 50px;
    min-width: 100px;
    background-color: #c00000;
    color: white;
    line-height: 50px;
    text-align: center;
    padding: 0 10px;
  }
}
</style>
